<template>
	<div>
		<!-- 搜索区域 -->
		<div class="search_box">
			<el-form ref="searchFormRef" :model="queryInfo" label-width="120px" class="search-box">
				<el-row :gutter="24">
					<el-col :span="6">
						<el-form-item prop="order_no" label="订单编号:">
							<el-input size="large" v-model.trim="queryInfo.order_no" placeholder="搜索订单编号"
								:prefix-icon="Search" @change="searchTable"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="6">
						<el-form-item prop="status" label="订单状态:">
							<Select width="240" placeholder="请选择订单状态" v-model="queryInfo.pay_status" :data="payStatusData"
								@change="searchTable" />
						</el-form-item>
					</el-col>

					<el-col :span="6">
						<div class="flex justify-end align-center">
							<el-button type="info" :icon="RefreshRight" size="large" @click="resetTable">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</div>

		<!-- 表格区域 -->
		<div class="margin-top">
			<el-table ref="multipleTableRef" :data="listData" style="width: 100%">
				<el-table-column type="selection" width="55" />

				<el-table-column property="order_no" label="订单编号"></el-table-column>

				<el-table-column property="order_no" label="商品信息">
          <template #default="scope">
						<div>商品名称：{{ scope.row.goods_name }}</div>
						<div>商品类别: {{ scope.row.goods_type_name }}</div>
						<div>商品价格: ¥{{ scope.row.one_money }}</div>
						<div>商品数量： {{ scope.row.order_number }}</div>
					</template>
        </el-table-column>

				<el-table-column label="订单金额">
					<template #default="scope">
						<div>用户：{{ scope.row.uname }}</div>
						<div>¥ {{ scope.row.one_money }}</div>
						<div>数量： {{ scope.row.order_number }}</div>
					</template>
				</el-table-column>

				<el-table-column property="status" label="状态">
					<template #default="scope">
						<template v-for="item in payStatusData">
							<el-tag class="ml-2" :key="item.id" :color="item.color"
								v-if="scope.row.pay_status === item.value">
								{{ item.label }}
							</el-tag>
						</template>
					</template>
				</el-table-column>

				<el-table-column property="create_time" label="下单时间" />

			</el-table>
		</div>

		<!-- 分页区域 -->
		<div class="margin-top">
			<Pagination :total="total" @change="changePage" />
		</div>

		<!-- 产品表单模态框 -->
		<ProductFormModal v-model:visible="productModalVisible" :productData="currentProduct" :isEdit="isEditMode"
			@submit="handleProductSubmit" />

		<!-- 添加价格弹窗 -->
		<ProductPriceFormModal ref="productPriceFormModalRef" @reload="searchTable" />
	</div>
</template>

<script lang="ts" setup>
	import dayjs from 'dayjs'
	import { ref, reactive, computed, toRef, onMounted, watch } from 'vue'
	import Select from '@/components/admin/Select.vue'
	import { useRoute } from 'vue-router'
	import useTableHook from '@/hooks/useTableHook'
	import { GetOrderListApi } from '@/api/index'
	import { RefreshRight } from '@element-plus/icons-vue'
	import ProductFormModal from '@/components/admin/merchants/ProductFormModal.vue'
	import ProductPriceFormModal from '@/components/admin/merchants/ProductPriceFormModal.vue'
	import { ElMessage, ElMessageBox } from 'element-plus'
	import Pagination from '@/components/admin/Pagination.vue'

  const props = defineProps({
    userId: {
      type: String,
      default: ''
    }
  })

	const statusData = [
		{
			label: '全部状态',
			value: ''
		},
		{
			label: '待发货',
			value: 0,
			color: '#f4f4f5'
		},
		{
			label: '发货中',
			value: 1,
			color: '#409eff'
		},
		{
			label: '代签收',
			value: 2,
			color: '#e6a23c'
		},
		{
			label: '已签收',
			value: 3,
			color: '#66c03a'
		},
		{
			label: '结束订单',
			value: 4,
			color: '#909399'
		},
		{
			label: '取消订单',
			value: 5,
			color: '#f56c6c'
		}
	]
	const payStatusData = [
		{
			label: '待付款',
			value: 0,
			color: ''
		},
		{
			label: '付款成功',
			value: 2,
			color: 'rgba(89,255,64,0.55)'
		},
		{
			label: '取消付款',
			value: 3,
			color: '#e6a23c'
		},
	]
	const queryInfo = ref({
		order_no: '',
		dates: [],
		start_time: '',
		end_time: '',
		status: '',
    shopId: '',
		page: 1,
		limit: 10,
	})

	const searchFormRef = ref<FormInstance>()

	// 重置表格
	const resetTable = () => {
		searchFormRef.value.resetFields()

    queryInfo.shopId = props.userId

		queryInfo.value.start_time = ''

		queryInfo.value.end_time = ''

		console.log(1111, queryInfo.value)

		searchTable()
	}

	const changeDate = (record) => {
		queryInfo.value.start_time = queryInfo.value.dates[0]
		queryInfo.value.end_time = queryInfo.value.dates[1]
		searchTable()
	}

	onMounted(() => {
    queryInfo.value.shopId = props.userId

		initTable()
	})

	const {
		total,
		listData,
		searchTable,
		initTable,
		changePage,
	} = useTableHook(queryInfo.value, GetOrderListApi)
</script>

<style lang="scss" scoped>
	/* 头部区域样式 */
	.page-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
		flex-wrap: wrap;
		gap: 15px;
	}

	.title-section {
		display: flex;
		flex-direction: column;
		gap: 5px;
	}

	.page-title {
		font-size: 24px;
		font-weight: 600;
		margin: 0;
		color: #2c3e50;
	}

	.merchant-name {
		color: #e74c3c;
	}

	.back-link {
		display: inline-flex;
		align-items: center;
		font-size: 14px;
		color: #7f8c8d;
		text-decoration: none;
		transition: color 0.3s;
		margin-top: 5px;
	}

	.back-link:hover {
		color: #e74c3c;
	}

	.back-link i {
		margin-right: 5px;
	}

	.header-actions {
		display: flex;
		gap: 10px;
	}

	.btn-primary {
		background-color: #e74c3c;
		color: white;
		border: none;
		padding: 10px 15px;
		border-radius: 4px;
		cursor: pointer;
		display: flex;
		align-items: center;
		font-weight: 600;
		transition: background-color 0.3s;
	}

	.btn-primary:hover {
		background-color: #c0392b;
	}

	.btn-primary i {
		margin-right: 5px;
	}
</style>
